﻿#region

using System;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLogicLayer;
using BusinessObjects;

#endregion

namespace FastTrackCabService.AdminPanel
{
    public partial class EditDriverForm : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int staffId;
                if (int.TryParse(Request.QueryString["staffId"], out staffId))
                {
                    foreach (var subsidiary in SubsidiaryBLL.GetAllSubsidiaries().OrderBy(sub => sub.CityId))
                    {
                        var city = CityBLL.GetCityById(subsidiary.CityId);
                        var item = new ListItem(city.Name, subsidiary.SubsidiaryId.ToString());
                        ddlSubsidiary.Items.Add(item);
                    }

                    var staff = StaffBLL.GetStaffById(staffId);
                    var driver = new Driver(staff)
                    {
                        LicenseNo = DriverBLL.GetLicenceNo(staffId)
                    };

                    tbFName.Text = driver.FirstName;
                    tbLName.Text = driver.LastName;
                    tbEMail.Text = driver.EMail;
                    tbTelephone.Text = driver.TelephoneNo;
                    tbAddress.Text = driver.Address;
                    tbLicense.Text = driver.LicenseNo;

                    foreach (ListItem item in ddlSubsidiary.Items)
                    {
                        if (item.Value == driver.SubsidiaryId.ToString())
                        {
                            item.Selected = true;
                            break;
                        }
                    }
                }
                else
                {
                    Response.Redirect("Drivers.aspx");
                }
            }
        }

        protected void tbEMail_OnTextChanged(object sender, EventArgs e)
        {
            cvEMail.Validate();
        }

        protected void cvEMail_OnServerValidate(object source, ServerValidateEventArgs args)
        {
            var staff = StaffBLL.GetStaffById(Convert.ToInt32(Request.QueryString["staffId"]));
            args.IsValid = tbEMail.Text == staff.EMail || !StaffBLL.IsEMailTaken(tbEMail.Text);
        }

        protected void btnReset_OnClick(object sender, EventArgs e)
        {
            Server.Transfer("EditDriverForm.aspx?staffId=" + Request.QueryString["staffId"]);
        }

        protected void btnRegister_OnClick(object sender, EventArgs e)
        {
            if (IsValid)
            {
                var staffId = Convert.ToInt32(Request.QueryString["staffId"]);
                StaffBLL.EditStaff(staffId, tbFName.Text, tbLName.Text, tbEMail.Text, tbTelephone.Text, tbAddress.Text,
                    "Supervisor", Convert.ToByte(ddlSubsidiary.SelectedValue));
                DriverBLL.EditDriver(staffId, tbLicense.Text);
                Response.Redirect("DriverFullView.aspx?staffId=" + staffId);
            }
        }
    }
}